/*
 * Copyright 2012 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.android.calllog.util;

import java.util.Map;

import nl.biz_e.android.calllog.CallLogApp;
import nl.biz_e.android.calllog.R;
import nl.biz_e.android.calllog.dao.CalendarDao;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.provider.ContactsContract.CommonDataKinds.Phone;

/**
 * Utility class to store and retrieve the preferences for this app.
 * 
 * @author Joost A. Bloemsma
 */
public class CallLogPrefs {
	/**
	 * The name the preferences for this app will be stored under
	 */
	public static final String PREFS_NAME = "nl.biz_e.android.calllog.util.CallLogPrefs";
	
	/**
	 * A placeholder that can be used in the Title template. When an event is generated, 
	 * this placeholder will be replaced with the contacts name.
	 */
	public static final String NAME_PLACEHOLDER = "{name}";
	
	/**
	 * Checks if the service is running (according to preferences)
	 * @return {@code true} if the service is currently running
	 */
	public static boolean getServiceRunning() {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		return prefs.getBoolean(ctx.getString(R.string.prefs_service_running_key), false);
	}
	
	/**
	 * Stores whether the service should be running
	 * @param value
	 */
	public static void setServerRunning(boolean value) {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		Editor edit = prefs.edit();
		edit.putBoolean(ctx.getString(R.string.prefs_service_running_key), value);
		edit.commit();
	}
	
	/**
	 * Returns the interval the service is polling the call log for changes
	 * @return interval in minutes
	 */
	public static String getServiceInterval() {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		return prefs.getString(ctx.getString(R.string.prefs_service_interval_key), "0");
	}
	
	/**
	 * Converts a value of the service interval to the text displayed in the preference screen
	 * @return String containing a readable text representation.
	 */
	public static String getServiceIntervalText() {
		Context ctx = CallLogApp.getAppContext();
		String[] values = ctx.getResources().getStringArray(R.array.prefs_service_interval_values);
		String[] names = ctx.getResources().getStringArray(R.array.prefs_service_interval);
		String interval = getServiceInterval();
		int i = 0;
		for (String value : values) {
			if (interval.equals(value)) {
				return names[i];
			}
			i++;
		}
		return ctx.getString(R.string.prefs_unset);
	}
	
	/**
	 * Returns the name of the currently used calendar
	 * @return The name of the currently selected calendar
	 */
	public static String getCurrentCalendarName() {
		Long id = getCalendarId();
		Map<Long, String> calendars = CalendarDao.getInstance().getCalendars(false);
		return calendars.get(id);
	}
	
	/**
	 * Sets the interval the service is polling the call log for changes
	 * @param value interval in minutes
	 */
	public static void setServiceInterval(String value) {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, 0);
		Editor edit = prefs.edit();
		edit.putString(ctx.getString(R.string.prefs_service_interval_key), value);
		edit.commit();
	}
	
	/**
	 * Returns the id of the currently used calendar
	 * @return ID of the currently used calendar
	 */
	public static Long getCalendarId() {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		String key = ctx.getString(R.string.prefs_calendar_calendar_key);
		String pref = prefs.getString(key, "0");
		Long id = Long.decode(pref);
		return id;
	}
	
	/**
	 * Sets the calendar id to use
	 * @param id
	 */
	public static void setCalendarId(long id) {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, 0);
		Editor edit = prefs.edit();
		edit.putLong(ctx.getString(R.string.prefs_service_interval_key), id);
		edit.commit();
	}
	
	/**
	 * Returns the title template
	 * @return String
	 */
	public static String getTitleTemplate() {
		Context ctx = CallLogApp.getAppContext();
		SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
		String title = prefs.getString(ctx.getString(R.string.prefs_calendar_title_usertempl_key), "");
		if (title.length() == 0) {
			title = ctx.getString(R.string.caltxt_title_templ);
		}
		return title;
	}
	
	/**
	 * Maps the number type to a human readable String. 
	 * @param type
	 * @return Description of the number type
	 */
	public static String getNumberTypeName(int type) {
		Context ctx = CallLogApp.getAppContext();
		String name = null;
		switch (type) {
			case Phone.TYPE_HOME:
				 name = ctx.getString(R.string.numbertype_home);
				 break;
			case Phone.TYPE_MOBILE:
				name = ctx.getString(R.string.numbertype_mobile);
				break;
			case Phone.TYPE_WORK:
				name = ctx.getString(R.string.numbertype_work);
				break;
			case Phone.TYPE_COMPANY_MAIN:
				name = ctx.getString(R.string.numbertype_company);
				break;
			default:
				name = ctx.getString(R.string.numbertype_other);
				break;
		}
		return name;
	}
}

